﻿using JewererBasic.BO;
using JewererBasic.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JewererBasic.Controllers
{
    public class ProcessReportController : Controller
    {
        MSQDBEntities db = new MSQDBEntities();
        private static List<ProcessReportModel> listProcesses;

        public ActionResult Index()
        {
            ViewBag.ListKinds = this.db.KINDs.ToList();
            listProcesses = ProcessReportBO.GetListOfProcesses();

            return View(listProcesses);
        }

        [HttpPost]
        public ActionResult Index(FormCollection fc)
        {
            ViewBag.ListKinds = this.db.KINDs.ToList();

            string orderId = fc["txtOrderId"];
            string finishWeekStr = fc["txtFinishWeek"];
            string productId = fc["txtProductId"];
            string kindIdStr = fc["txtKind"];

            int finishWeek;
            if (Int32.TryParse(finishWeekStr, out finishWeek) == false)
            {
                finishWeek = 0;
            }
            int kindId;
            if (Int32.TryParse(kindIdStr, out kindId) == false)
            {
                kindId = 0;
            }

            listProcesses = ProcessReportBO.GetListOfProcesses(orderId, finishWeek, kindId, productId);

            return View(listProcesses);
        }

        public ActionResult BagsInProcess(int processId)
        {
            ProcessReportModel process;

            if (listProcesses == null)
            {
                return RedirectToAction("Index");
            }
            else
            {
                process = listProcesses.Find(m => m.ProcessId == processId);
            }

            if (process == null)
            {
                return RedirectToAction("Index");
            }

            List<BAG> listOfBags = new List<BAG>();
            // get bags from bagids
            foreach (var id in process.BagIds)
            {
                listOfBags.Add(this.db.BAGs.Find(id));
            }

            return View(listOfBags);
        }
    }
}
